import xhx_images from './xhx_images.js'
import xhx_route from './xhx_route.js'
import Package from './utils/Package.js'
import middleware from './utils/middleware.js'
import uploadFile from './utils/uploadFile.js'
import downloadFile from './utils/downloadFile.js'
import Login from "./utils/Login.js"
import utils from "./utils/utils.js"
import xLog from './utils/xLog.js'
import serAddrs from './wxjs/serAddrs.js'
import navToNext from './wxjs/navToNext.js'
import QQMapWX from './utils/qqmap-wx-jssdk.min.js'
import uploadAliyun from "./UploadAliyun/uploadAliyun";

var currentApp = {
  xhx_images,
  xhx_route,
  Package,
  middleware,
  uploadFile,
  uploadAliyun,
  downloadFile,
  notifyCount: null,
  mainColor: "#FF9800",
  sportData: { //运动数据中心小程序配置参数
    appid: "wxf4469b3920bc1ed2", //appid
    path: 'pages/login/login', //跳转页面路径
  },
  whochangeSport: { //谁变运动小程序配置参数
    appid: "wx2766b2272b434d9a", //appid
    path: 'pages/order/list/list', //跳转页面路径
  },
  appType: 1, //小程序类型,1=快乐健身，3=运动数据中心
  main: {
    name: "快乐健身",
    version: "3.0.1",
  },
  sys: {},
  userInfo: {

  },
  invite: {}, //邀请参数
  createChallegeParam:{},  //创建自建赛的提交表单

  serAddrs,
  navToNext,
  onLaunch,
  onShow,
  ifiPhone,
  isNavigateToLogin, // 微信未登录提示及跳转(登录时返回信息，未登录时跳转到登录页面)
  isNavigateToBind,
  passIsSet, //是否有设置密码
  getNotifyCount,
  globalData: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight']
  }
};

var app = Object.assign(xLog, utils, currentApp);

App(app)

function onLaunch() {
  const that = this;
  const sys = wx.getSystemInfoSync();
  that.sys = sys;
  // 实例化API核心类
  that.qqmapsdk = new QQMapWX({
    key: 'K36BZ-VOMHD-UYJ4Q-PC6NS-NFM3E-SBFXG'
  });

  that.xlogin = new Login(that);
  that.xlogin.getUser();
}

function onShow() {
  const that = this;
  if (that.userInfo.userId) {
    that.notifyCount = null;
    // that.getNotifyCount();
  }


  const updateManager = wx.getUpdateManager();
  updateManager.onCheckForUpdate(function(res) {
    // console.log('请求完新版本信息的回调', res.hasUpdate)
  })

  updateManager.onUpdateReady(function() {
    wx.showModal({
      title: '更新提示',
      content: '新版本已经准备好，是否重启应用？',
      success: function(res) {
        if (res.confirm) {
          // 新的版本已经下载好，调用 applyU-pdate 应用新版本并重启
          updateManager.applyUpdate()
        }
      }
    })

  })

  updateManager.onUpdateFailed(function() {
    console.log('新的版本下载失败');
  })
}

// 是否为刘海屏(暂只判断iOS)
function ifiPhone() {
  const that = this;
  var systemInfo = wx.getSystemInfoSync();
  console.log(systemInfo)
  var statusBarHeight = systemInfo.statusBarHeight;
  var model = systemInfo.model.slice(0, 6);
  if (model == "iPhone") {
    return true;
  }
  return false;
}

function isNavigateToLogin() {
  const that = this;
  const userInfo = this.userInfo;
  let userId = userInfo && userInfo.userId || "";
  if (!userId) {
    // wx.showModal({
    //   title: '未登录',
    //   content: '确定登录？',
    //   success: function(res) {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: that.xhx_route.login
    //       })
    //     }
    //   }
    // })
    wx.navigateTo({
      url: that.xhx_route.login
    })
    return false;
  }
  return userInfo;
}

function isNavigateToBind() {
  const that = this;
  const userInfo = this.userInfo;
  let telephone = userInfo && userInfo.telephone || "";
  if (!telephone) {
    wx.showModal({
      title: '提示',
      content: '未绑定，确定绑定手机号？',
      success: function(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: that.xhx_route.mineBind
          })
        }
      }
    })
    return false;
  }
  return userInfo;
}

function passIsSet() {
  const that = this;
  const status = that.userInfo.passIsSet;
  if (status != 1) {

    wx.showToast({
      title: '请设置密码',
      icon: "none",
      duration: 2000
    })

    setTimeout(() => {
      wx.switchTab({
        url: xhx_route.mineBase,
      })
    }, 2000)

    return false;
  }

  return true;
}

var num = 0;

function getNotifyCount() {
  const that = this;
  middleware.requestAjax({
    serAddr: 'happyuserservice/tPrivateLetter/getMyUnreadPrivateLetter',
    cancelToast: true,
    success: function(res) {
      that.notifyCount = res.data || 0;
      console.log(res)
      if (that.notifyCount > 0) {
        console.log("有消息")
        wx.showTabBarRedDot({
          index: 2,
        })
      } else {
        console.log("没有消息")
        wx.hideTabBarRedDot({
          index: 2,
        })
      }
    },
    fail: function() {
      num++;
      if (num < 3) that.getNotifyCount();
    }
  })
}